package com.lc.zpyh.ui.activity.mine;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.request.AddAddersseApi;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.request.SelectCabinetryBySchoolApi;
import com.lc.zpyh.http.request.UpdateAdderssApi;
import com.lc.zpyh.http.response.SelectAddressServiceeBean;
import com.lc.zpyh.http.response.SelectCabinetryBySchoolBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.util.SPUtil;
import com.lc.zpyh.view.JiGuiPopupWindow;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddAddressActivity extends AppActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.switch_accelerate)
    Switch switchAccelerate;
    @BindView(R.id.rl_check_address)
    RelativeLayout rlCheckAddress;
    @BindView(R.id.tv_guiji)
    TextView tvGuiji;
    private JiGuiPopupWindow jiGuiPopupWindow;
    private String cabinetryId="0";
    private boolean isUpdate;
    private SelectAddressServiceeBean.ListBean dataBean;
    private String addressId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_addaddress;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        isUpdate=getIntent().hasExtra("MineAddressBean");
        if(isUpdate){
            dataBean = (SelectAddressServiceeBean.ListBean) getIntent().getSerializableExtra("MineAddressBean");
            addressId = dataBean.getTheGoodsId()+"";
            cabinetryId = dataBean.getCabinetryId()+"";
            etName.setText(dataBean.getName());
            etPhone.setText(dataBean.getPhone());

            tvGuiji.setText(dataBean.getCabinetryName());
            if(dataBean.getPatientia()==1){
                switchAccelerate.setChecked(true);
            }else {
                switchAccelerate.setChecked(false);
            }
        }

        selectCabinetryBySchool();
    }
    private void UpdateAddress() {
        if (etPhone.getText().toString().length() != 11) {
            toast(R.string.common_phone_input_error);

            return;
        }
        if (etName.getText().toString().isEmpty()) {
            toast("姓名不能为空");

            return;
        }
        EasyHttp.post(this)
                .api(new UpdateAdderssApi().setSchooleId(SPUtil.get(this, IntentKey.ADDRESSID, "")).setUserId(SPUtil.get(this, IntentKey.USERID, "")).setName(etName.getText().toString()).setPhone(etPhone.getText().toString()).setAddressid(addressId).setPatientia(switchAccelerate.isChecked() ? "1" : "0").setCabinetryId(cabinetryId)).request(new HttpCallback<PublicMsgBean>(this) {

            @Override
            public void onSucceed(PublicMsgBean data) {
//
                toast(data.getList());
                finish();

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }
    private void addAdderss() {
        if (etPhone.getText().toString().length() != 11) {
            toast(R.string.common_phone_input_error);

            return;
        }
        if (etName.getText().toString().isEmpty()) {
            toast("姓名不能为空");

            return;
        }
        if (cabinetryId.equals("0")) {
            toast("请选择柜机");

            return;
        }
        EasyHttp.post(this)
                .api(new AddAddersseApi().setSchooleId(SPUtil.get(this, IntentKey.ADDRESSID, "")).setUserId(SPUtil.get(this, IntentKey.USERID, "")).setName(etName.getText().toString()).setPhone(etPhone.getText().toString()).setPatientia(switchAccelerate.isChecked() ? "1" : "0").setCabinetryId(cabinetryId)).request(new HttpCallback<PublicMsgBean>(this) {

            @Override
            public void onSucceed(PublicMsgBean data) {
//
                toast(data.getList());
                finish();

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    @OnClick({R.id.rl_check_address, R.id.rl_add_address})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_check_address:

                if (jiGuiPopupWindow.isShowing()) {
                    jiGuiPopupWindow.dismiss();
                } else {
                    if (jiGuiPopupWindow != null) {
                        WindowManager.LayoutParams lp = getWindow().getAttributes();
                        lp.alpha = 0.7f;
                        getWindow().setAttributes(lp);
                        jiGuiPopupWindow.showAsDropDown(rlCheckAddress, 0,0,Gravity.RIGHT);
                    }
                }
                break;
            case R.id.rl_add_address:
                if(isUpdate){
                    //修改地址
               UpdateAddress();
                }else {
                    //新增地址
                    addAdderss();
                }

                break;
        }
    }



    private void selectCabinetryBySchool() {
        EasyHttp.post(this)
                .api(new SelectCabinetryBySchoolApi().setSchoolid(SPUtil.get(this, IntentKey.ADDRESSID, ""))).request(new HttpCallback<SelectCabinetryBySchoolBean>(this) {

            @Override
            public void onSucceed(SelectCabinetryBySchoolBean data) {
//
                if (data.getCode() == 0) {

                    jiGuiPopupWindow = new JiGuiPopupWindow(getContext(), data.getList(), Integer.parseInt(cabinetryId));

                    jiGuiPopupWindow.setOnItemLickenter(new JiGuiPopupWindow.OnItemLickenter() {
                        @Override
                        public void onTypeClick(String type, String id) {
                            tvGuiji.setText(type);
                            cabinetryId = id;
                        }
                    });
                }


            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }
}
